
package com.smartfit;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_main );
		
		Button btnRegister = ( Button )findViewById( R.id.RegisterButton );
		btnRegister.setOnClickListener(
				new OnClickListener()
				{
					@Override
					public void onClick( View arg0 )
					{
						// TODO Auto-generated method stub
						
						Intent registerActivity = new Intent( MainActivity.this , RegisterActivity.class );
						
						startActivity( registerActivity );
						
					}
				}
			);

		Button btnCompare = ( Button )findViewById( R.id.CompareButton );
		btnCompare.setOnClickListener(
				new OnClickListener()
				{
					@Override
					public void onClick( View arg0 )
					{
						// TODO Auto-generated method stub
						
						Intent compareActivity = new Intent( MainActivity.this , CompareActivity.class );
						
						startActivity( compareActivity );
						
					}
				}
			);
		
		CheckDefaultPreferences();

		SmartFitApp app = ( ( SmartFitApp )getApplicationContext() );
		
		if ( app.m_ExamplarDB.Initialize() == false )
		{
			Toast.makeText( this , "Failed initialize" , Toast.LENGTH_SHORT ).show(); 
		}
		
		LoadPreferences();
	}

	@Override
	protected void onActivityResult( int requestCode , int resultCode , Intent data )
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if ( resultCode == RESULT_OK )
		{
			LoadPreferences();
		}
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu )
	{

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate( R.menu.main , menu );
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected( MenuItem item )
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
	
		if ( id == R.id.action_settings )
		{
			Intent i = new Intent( MainActivity.this , PreferencesActivity.class );
			startActivityForResult( i , 0 );
			
			return true;
		}
		
		return super.onOptionsItemSelected( item );
	}
	
	private void CheckDefaultPreferences()
	{
		SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences( this );
		
		if ( mySharedPreferences.contains( "set_option_include_gravity" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putBoolean("set_option_include_gravity" , true );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_apply_kalman_filter" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putBoolean("set_option_apply_kalman_filter" , true );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_apply_square" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putBoolean("set_option_apply_square" , true );
			editor.commit();
		}
		
		if ( mySharedPreferences.contains( "set_option_keep_frame" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putBoolean("set_option_keep_frame" , true );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_noise_value" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString("set_option_noise_value" , "0" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_max_num_examplar" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString("set_option_max_num_examplar" , "3" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_examplar_compare_angle_threshold" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString( "set_option_examplar_compare_angle_threshold" , "50" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_examplar_compare_distance_threshold" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString( "set_option_examplar_compare_distance_threshold" , "10" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_enable_check_start" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putBoolean( "set_option_enable_check_start" , true );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_check_start_constant1" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString( "set_option_check_start_constant1" , "0.5" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_check_start_constant2" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString( "set_option_check_start_constant2" , "50" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_check_start_threshold" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString( "set_option_check_start_threshold" , "50" );
			editor.commit();
		}

		if ( mySharedPreferences.contains( "set_option_pattern_compare_threshold" ) == false )
		{
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			
			editor.putString( "set_option_pattern_compare_threshold" , "0.5" );
			editor.commit();
		}
	}
	
	private void LoadPreferences()
	{
		String s;
		SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences( this );
		
		SmartFitApp app = ( ( SmartFitApp )( getApplicationContext() ) );
		
		boolean bIsContainedGravity = mySharedPreferences.getBoolean( "set_option_contain_gravity" , true );
		app.m_ExamplarDB.SetGravityContain( bIsContainedGravity );

		boolean bIsAppliedKalmanFilter = mySharedPreferences.getBoolean( "set_option_apply_kalman_filter" , true );
		app.m_ExamplarDB.SetKalmanFilterApply( bIsAppliedKalmanFilter );

		boolean bIsAppliedSquare = mySharedPreferences.getBoolean( "set_option_apply_square" , true );
		app.m_ExamplarDB.SetSquareApply( bIsAppliedSquare );

		boolean bIsKeepFrame = mySharedPreferences.getBoolean( "set_option_keep_frame" , true );
		app.m_ExamplarDB.SetFrameKeep( bIsKeepFrame );

		s = mySharedPreferences.getString( "set_option_noise_value" , "0" );
		double fNoise = Double.valueOf( s );
		app.m_ExamplarDB.SetNoiseValue( fNoise );

		s = mySharedPreferences.getString( "set_option_max_num_examplar" , "3" );
		int iMaxNumExamplars = Integer.valueOf( s );
		app.m_ExamplarDB.SetNumMaxExamplars( iMaxNumExamplars );
		
		s = mySharedPreferences.getString( "set_option_examplar_compare_angle_threshold" , "50" );
		int iAngleThreshold = Integer.valueOf( s );
		app.m_ExamplarDB.SetAngleThreshold( iAngleThreshold );

		s = mySharedPreferences.getString( "set_option_examplar_compare_distance_threshold" , "10" );
		int iDistanceThreshold = Integer.valueOf( s );
		app.m_ExamplarDB.SetDistanceThreshold( iDistanceThreshold );

		boolean bIsEnableCheckStart = mySharedPreferences.getBoolean( "set_option_enable_check_start" , true );
		app.m_ExamplarDB.SetEnableCheckStart( bIsEnableCheckStart );

		s = mySharedPreferences.getString( "set_option_check_start_constant1" , "0.5" );
		double fCheckStartConstant1 = Float.valueOf( s );
		app.m_ExamplarDB.SetCheckStartConstant1( fCheckStartConstant1 );

		s = mySharedPreferences.getString( "set_option_check_start_constant2" , "50" );
		double fCheckStartConstant2 = Float.valueOf( s );
		app.m_ExamplarDB.SetCheckStartConstant2( fCheckStartConstant2 );

		s = mySharedPreferences.getString( "set_option_check_start_threshold" , "50" );
		double fCheckStartThreshold = Float.valueOf( s );
		app.m_ExamplarDB.SetCheckStartThreshold( fCheckStartThreshold );

		s = mySharedPreferences.getString( "set_option_pattern_compare_threshold" , "0.5" );
		double fCompareThresholdForPattern = Float.valueOf( s );
		app.m_ExamplarDB.SetCompareThresholdForPattern( fCompareThresholdForPattern );
	}
}